<?php

namespace App\Utility;

use GuzzleHttp\Client;

class UdunUtility
{
    static private $host = 'https://sg02.udun.io';
    static private $merchantId = '313197';
    static private $apiKey = 'e65293c82175cb1b00d7f44902bbc324';

    public function createAddress(string $coin_code){
        
        $url = self::$host.'/mch/address/create';
        $body = array('merchantId'=>self::$merchantId, 'mainCoinType'=>$coin_code, 'callUrl'=>'https://www.targetmall.co/udunCallback');
        $params = [
            'timestamp'=>time(),
            'nonce'=>rand(111111,999999),
            'body' => json_encode(array($body)),
        ];
        
        $params['sign'] = self::getSign($params);
        
        //发起请求
        $client = new Client();
        $res = $client->request('POST', $url, ['json' => $params]);
        
        //return $res->getBody()->getContents();
        $res = json_decode($res->getBody()->getContents(), true);
        return $res;
        dump($res);
        
        exit;
        return json_decode($res->getBody()->getContents(), true);
    }
    
    
    //
    static private function getSign($params){
        return strtolower(md5($params['body'] . self::$apiKey . $params['nonce'] . $params['timestamp']));
    }


    public function withdraw(string $orderid,string $address,string $amount,string $usdt_type)
    {

        $url = 'http://206.238.42.42/testApi.php';


        $params = [
            'orderid' => $orderid,
            'address' => $address,
            'amount' => $amount,
            'method' => 'withdraw',
            'usdt_type' => $usdt_type,
        ];

        //发起请求
        $client = new Client();
        $res = $client->request('POST', $url, [
            'json' => $params
        ]);
        //return $res->getBody()->getContents();
        return json_decode($res->getBody()->getContents(), true);
    }

}
